package org.example.executor;

import org.example.executor.statement.StatementHandler;
import org.example.mapping.BoundSql;
import org.example.mapping.MappedStatement;
import org.example.session.Configuration;
import org.example.session.ResultHandler;
import org.example.transaction.Transaction;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * @Author: luyg
 * @Date: 2024/12/03/23:02
 * @Description:
 */
public class SimpleExecutor extends BaseExecutor{

    public SimpleExecutor(Configuration configuration, Transaction transaction) {
        super(configuration, transaction);
    }

    @Override
    protected <E> List<E> doQuery(MappedStatement ms, Object parameter, ResultHandler resultHandler, BoundSql boundSql) {
        try {
            Configuration configuration = ms.getConfiguration();
            StatementHandler statementHandler = configuration.newStatementHandler(this, ms, parameter, resultHandler, boundSql);
            Connection connection = transaction.getConnection();
            Statement prepare = statementHandler.prepare(connection);
            statementHandler.parameterize(prepare);
            return statementHandler.query(prepare,resultHandler);
        }catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
